using System.Collections.Generic;
using UnityEditor;
using UnityEngine;


namespace XYTerrain.Editor
{
    
[CreateAssetMenu(menuName = "地形/地形音效配置")]
public class TerrainSoundConfig : ScriptableObject
{
        public string[] Names
        {
            get
            {
                return nameList.ToArray();
            }
        }
        public List<string> nameList = new List<string>();
        public List<LayerDataPlus> LayerSoundDataList = new List<LayerDataPlus>();
        public Dictionary<int, LayerDataPlus> soundTable;

        public void WarmUpTable()
        {
            soundTable = new Dictionary<int, LayerDataPlus>();
            foreach (var i in LayerSoundDataList)
            {
               soundTable.Add(i.layer.GetInstanceID(),i); 
            } 
        }

        public int GetSoundId(int layerID)
        {
            if(soundTable.ContainsKey(layerID)) return soundTable[layerID].soundIndex;
            return -1;
        }

        public bool ExistLayer(TerrainLayer layer)
        {
         return   LayerSoundDataList.Exists((x) => x.layer == layer);
        }

    }


    [CustomEditor(typeof(TerrainSoundConfig))]

    public class TerrainSoundConfigEditor:UnityEditor.Editor
    {
        public TerrainSoundConfig sound;
        private List<string> removeList  ;
        public List<LayerDataPlus> layerDataRemoveList;
        private bool showNamesConfig = false;
        private bool showLayersConfig = false;
        public bool isDirty;
        
        public  void OnEnable()
        {
            sound  = target as TerrainSoundConfig;
            removeList = new List<string>();
            layerDataRemoveList = new List<LayerDataPlus>();
        }
        
        
        

        public override void OnInspectorGUI()
        {

            
             showLayersConfig = EditorGUILayout.Foldout(showLayersConfig,"地形配置");
             if(showLayersConfig) DrawLayerDataGroup();

            showNamesConfig = EditorGUILayout.Foldout(showNamesConfig,"音频配置编辑");
            if(showNamesConfig) DrawNamesList();

            if (isDirty)
            {
                EditorUtility.SetDirty(sound);
                AssetDatabase.SaveAssets();
                isDirty = false;
            }
            
        }

        private void DrawLayerDataGroup()
        {
            foreach (var i in sound.LayerSoundDataList)
            {
               DrawLayerData(i); 
            }

            if (GUILayout.Button("+"))
            {
                sound.LayerSoundDataList.Add( new LayerDataPlus());
                isDirty = true;
            }
            CheckLayerDataRemove();
        }


        private void CheckLayerDataRemove()
        {
            if (layerDataRemoveList.Count > 0)
            {
                foreach (var i in layerDataRemoveList)
                {
                    sound.LayerSoundDataList.Remove(i);
                }
                layerDataRemoveList.Clear();
                isDirty = true;
            }
        }



        private void InitNameList()
        {
            if (sound.nameList == null || sound.nameList.Count == 0)
            {
                sound.nameList = new List<string>();
                sound.nameList.Add("无");
            }
        }

        private TerrainLayer templayer; 
        
        private void DrawLayerData(LayerDataPlus data)
        {
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.BeginVertical(EditorStyles.helpBox);
            
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Layer文件");
            EditorGUILayout.Space(-100);
            
            
            EditorGUI.BeginChangeCheck();
            templayer = EditorGUILayout.ObjectField(data.layer,typeof(TerrainLayer),true) as TerrainLayer;
            

            if (EditorGUI.EndChangeCheck())
            {
                if (templayer != null)
                {
                    if (!sound.ExistLayer(templayer))
                    {
                        data .layer= templayer;
                    }
                    else
                    {
                        Debug.Log("已经存在相同layer");
                        data.layer = null;
                    }
                }    
            }
            else
            {
                data.layer = templayer;
            }


            EditorGUILayout.Space(5);
            
            EditorGUILayout.EndHorizontal();
            
            if (data.layer != null)
            {
                EditorGUILayout.Space(10);
                EditorGUILayout.BeginHorizontal();
                TextureField( data.layer.diffuseTexture);


                EditorGUILayout.BeginVertical();
                EditorGUILayout.LabelField("音效类型");
                data.soundIndex = EditorGUILayout.Popup(data.soundIndex, sound.Names);
                EditorGUILayout.LabelField("当前音效ID:"+data.soundIndex);    
                EditorGUILayout.EndVertical();
                
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.Space(10);
            }
            
            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("x", GUILayout.MaxWidth(50)))
            {
               layerDataRemoveList.Add(data); 
            }
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.EndVertical();
            if (EditorGUI.EndChangeCheck())
            {
                isDirty = true;
            }
        }
        
        
        
        private  Texture2D TextureField( Texture2D texture)
        {
            GUILayout.BeginVertical();
            var style = new GUIStyle(GUI.skin.label);
            style.alignment = TextAnchor.UpperCenter;
            style.fixedWidth = 70;
            var result = (Texture2D)EditorGUILayout.ObjectField(texture, typeof(Texture2D), false, GUILayout.Width(70), GUILayout.Height(70));
            GUILayout.EndVertical();
            return result;
        }




        public void DrawNamesList()
        {

            InitNameList();
            EditorGUILayout.BeginVertical(EditorStyles.helpBox);
            EditorGUILayout.LabelField("音频ID");
            for (int i = 0; i<sound.Names.Length; i++)
            {
               DrawNameItem(i);
            }

            if (removeList.Count > 0)
            {
                foreach (var i in removeList)
                {
                    sound.nameList.Remove(i);
                }
                removeList.Clear();
            }
            
            if (GUILayout.Button("+"))
            {
                AddName();    
            }
            EditorGUILayout.EndVertical(); 
        }

        
        public void AddName()
        {
            sound.nameList.Add("");
            isDirty = true;
        }

        public void DrawNameItem(int index)
        {
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.BeginHorizontal(EditorStyles.helpBox);
            if (index == 0)
            {
                 EditorGUILayout.LabelField("名称: "+ sound.Names[index],GUILayout.MaxWidth(50));    
            }
            else
            {
                EditorGUILayout.LabelField("名称:",GUILayout.MaxWidth(50));    
                sound.nameList[index] = EditorGUILayout.TextField( sound.nameList[index],GUILayout.MaxWidth(200));
            }
            EditorGUILayout.LabelField("ID : " + index,GUILayout.MaxWidth(50));
            if (index != 0)
            {
                if( GUILayout.Button("X",GUILayout.Width(50)))
                {
                     removeList.Add(sound.nameList[index]); 
                }    
            }

            if (EditorGUI.EndChangeCheck())
            {
                isDirty = true;
            }
            EditorGUILayout.EndHorizontal();
            


        }
        
    }







    [System.Serializable]
    public class LayerDataPlus
    {
        public TerrainLayer layer;
        public string soundType;
        public int soundIndex;
    }


}
